<html>
<head><meta charset="utf-8"><title>Unsized pointers · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html">Unsized pointers</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209305242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209305242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209305242">(Sep 07 2020 at 15:05)</a>:</h4>
<p>What is an unsized pointer, like <code>*const [T]</code>? I assume it has the same representation as <code>&amp;[T]</code>, but there don't seem to be any functions to work with one, and <code>as</code> cast to/from references doesn't work either.</p>



<a name="209305392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209305392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209305392">(Sep 07 2020 at 15:06)</a>:</h4>
<p>In particular, I don't know how reasonable it is to cast between <code>*const T</code> and <code>*const [T]</code> in the same way as one would with C arrays, for example</p>



<a name="209305544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209305544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209305544">(Sep 07 2020 at 15:08)</a>:</h4>
<p>Or as an alternative to <code>transmute::&lt;&amp;[T], &amp;[T]&gt;(slice)</code> for lifetime erasure</p>



<a name="209315514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209315514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209315514">(Sep 07 2020 at 17:19)</a>:</h4>
<p>It's same as the relationship between <code>*const T</code> and <code>&amp;T</code> -- no lifetime, might not be valid/dereferencable, etc</p>



<a name="209315569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209315569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209315569">(Sep 07 2020 at 17:20)</a>:</h4>
<p>There are raw-parts constructors in the <code>ptr</code> module</p>



<a name="209315688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209315688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209315688">(Sep 07 2020 at 17:22)</a>:</h4>
<p>I think <code>&amp;[T]</code> should be able to cast <code>as *const [T]</code>, or even just coerce, but the other way is unsafe and must be explicit</p>



<a name="209315731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209315731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209315731">(Sep 07 2020 at 17:23)</a>:</h4>
<p><code>&amp;*ptr</code> like usual</p>



<a name="209316610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209316610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209316610">(Sep 07 2020 at 17:39)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">&amp;</span><span class="p">[</span><span class="n">T</span><span class="p">];</span><span class="w"> </span><span class="c1">// non-primitive cast: `*const [T]` as `&amp;[T]`</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209316783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209316783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209316783">(Sep 07 2020 at 17:42)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">ptr</span>::<span class="n">slice_from_raw_parts</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">len</span><span class="p">());</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>almost works but needs an unstable feature?</p>



<a name="209317895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209317895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209317895">(Sep 07 2020 at 18:03)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> which unstable feature?</p>



<a name="209317946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209317946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209317946">(Sep 07 2020 at 18:04)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/71146">https://github.com/rust-lang/rust/issues/71146</a></p>



<a name="209317981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209317981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209317981">(Sep 07 2020 at 18:05)</a>:</h4>
<p>It's blocked on <a href="https://github.com/rust-lang/rust/issues/73987">https://github.com/rust-lang/rust/issues/73987</a> but the reasoning doesn't make much sense to me, since it would apply equally to any method in the <code>std::ptr</code> namespace</p>



<a name="209319166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209319166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209319166">(Sep 07 2020 at 18:28)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="n">T</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">&amp;*</span><span class="n">x</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209319504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209319504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209319504">(Sep 07 2020 at 18:33)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Unsized.20pointers/near/209317981">said</a>:</p>
<blockquote>
<p>It's blocked on <a href="https://github.com/rust-lang/rust/issues/73987">https://github.com/rust-lang/rust/issues/73987</a> but the reasoning doesn't make much sense to me, since it would apply equally to any method in the <code>std::ptr</code> namespace</p>
</blockquote>
<p>does it? this is only really a footgun when the same method exists on raw ptrs and references.</p>



<a name="209319615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209319615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209319615">(Sep 07 2020 at 18:34)</a>:</h4>
<p>You can also call it as a free function if you are really worried (although that would require <code>&amp;raw</code> which I think is also not stable)</p>



<a name="209319874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209319874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209319874">(Sep 07 2020 at 18:37)</a>:</h4>
<p>Perhaps there should be an alias like <code>std::ptr::raw_slice_len(x: *const [T]) -&gt; usize</code> that can be standardized independently of <code>&lt;*const [T]&gt;::len()</code>?</p>



<a name="209666702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209666702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209666702">(Sep 10 2020 at 15:47)</a>:</h4>
<p>Shouldn't we standardize a generic method to get fat pointer meta data instead, e.g. something on the line of <code>std::ptr::get_fat_ptr_meta&lt;T: DST&gt;(x: *const T) -&gt; DST::PtrMeta</code> where <code>[T] impl DST { type PtrMeta = usize }</code> or so. This would also work with any wrapper of a slice and with trait objects (which have <code>PtrMeta=VTablePtr&lt;dyn Trait&gt;</code>, where <code>VTablePtr</code> is for now a opaque type like <code>struct VTablePtr&lt;T&gt;(*const u8, PhantomData&lt;T&gt;)</code> (note that the inner layout of <code>VTablePtr</code> is here just a example and non-pub)).</p>



<a name="209666977"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209666977" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kennytm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209666977">(Sep 10 2020 at 15:49)</a>:</h4>
<p>you're welcome to join <a href="https://github.com/rust-lang/lang-team/issues/55">https://github.com/rust-lang/lang-team/issues/55</a> <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="209667094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20pointers/near/209667094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20pointers.html#209667094">(Sep 10 2020 at 15:50)</a>:</h4>
<p>Thanks <span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>